package org.java.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.java.common.enums.ShoppingEnum;
import org.java.common.exception.ShoppingException;
import org.java.common.vo.ResultVo;
import org.java.item.dao.BrandMapper;
import org.java.item.entity.Brand;
import org.java.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WHQ
 * @version 1.0
 * @date 2022-3-5 0005 下午 3:16
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper brandMapper;
    @Override
    public ResultVo<Brand> getList(Integer page, Integer limit) {
        //开始分页
        PageHelper.startPage(page,limit);
        //得到查询的结果
        PageInfo<Brand> info=new PageInfo<>(brandMapper.selectAll());
        //判断查询到的结果是否为空
        if (info.getList().isEmpty()) {
            throw new ShoppingException(ShoppingEnum.BRAND_LIST_NOT_FOUND);
        }
        //创建vo对象封装数据
        ResultVo<Brand> vo=new ResultVo<>();
        vo.setCount(info.getTotal());
        vo.setData(info.getList());
        vo.setPageNum(page);
        vo.setMaxPage(info.getPages());
        return vo;
    }

    @Override
    public void del(Long id) {
        int rows = brandMapper.deleteByPrimaryKey(id);
        if (rows<=0){
            throw new ShoppingException(ShoppingEnum.BRAND_REMOVE_ERROR);
        }
    }
}
